function init(){
    //单选、全选按钮
    $(".product-list").on('click','s,i',function () {
        $(this).toggleClass('selected');
        var items=$('table i');
        var all=$('.chose-all');
        var item_count=0;
        if($(this).hasClass('chose-all')){
            if($(this).hasClass('selected')){
                $('.chose-all').addClass('selected');
                items.addClass('selected');
            }else{
                $('.chose-all').removeClass('selected');
                items.removeClass('selected');
            }
        }else{
        	$(items).each(function(){
        		if($(this).hasClass('selected')){
        			item_count ++;
        		}
        	});
        	if(item_count == items.length){
        		$('.chose-all').addClass('selected');
        	}else{
        		$('.chose-all').removeClass('selected');
        	}
        }
        subTotal();
    });
    
    //商品添加数量、结算相关
    $('.product-list').on('click','.product-item b',function(){
        var max=Number($(this).parent().siblings('.maxCount').val());
        var min=Number($(this).parent().siblings('.minCount').val());
        var price=Number($(this).parent().parent().siblings('.price').find('span').html());
        var count=Number($(this).siblings('input').val());
        var cartId = $(this).parent().parent().parent().attr('data-cartId');  
        if($(this).hasClass('add')){
            count++;
            if(count>max){
            	layer.msg("购买数量不能大于库存数量", {icon: 2,time:1000});
                count = max;
            }
        }else{
            count--;
            if(count<min){
            	layer.msg("购买数量不能小于起订量", {icon: 2,time:1000});
                count = min;
            }
        }
        $(this).siblings('input').val(count.toFixed(2));
        var total=(price*count).toString();
        $(this).parent().parent().siblings('.subtotal').children('span').html(cc(total)).attr('data-money',Number(total).toFixed(2));
        subTotal();
    });
  //去结算
    $('.check').click(function(){
    	if($(this).hasClass('go-check')){
    		var item=$('table i');
    		var priceId=[];
        	var list = [];
        	var isSubmit = 1;
        	$(item).each(function(){
        		if($(this).hasClass('selected')){
        			var item = {};
        			priceId.push($(this).parent().parent().attr('data-priceId'));
        			item.id = $(this).parent().parent().attr('data-id');
        			if(isPositiveInteger(Number($(this).parent().parent().find('.productCount').val()))){
        				item.quantity = $(this).parent().parent().find('.productCount').val();
        			}else{
        				layer.msg("请输入正确的商品数量", {icon: 2,time:1000});
        				isSubmit = 0;
        			}
        			list.push(item);
        		}
        	})
        	var cartModifyJson = JSON.stringify(list);
        	var priceIds = priceId.join(",");
        	console.log(cartModifyJson)
        	if(isSubmit == 1){
        		$.ajax({
     				type:"POST",
     				url:path+"/orderlang/userCart/update",
     				data:{"cartModifyJson":cartModifyJson},
     				success:function(data){
     					console.log(data);
     					if(data.state == 1){
     						window.location.href = path + "/orderlang/userCart/goBalance?priceId="+priceIds;
     					}
     				}
     			});
        	}
    	}
     });
//    加载购物车内商品
    $.ajax({
		type:"POST",
		url:path+"/orderlang/userCart/queryAllUserCart",
		success:function(data){
			if(data.state == 1){
				var list = data.data;
				var html = "";
				for(var i = 0;i<list.length;i++){
					html += "<tr data-priceId='"+list[i].prodPriceId+"' data-id='"+list[i].id+"'>";
					html += "<td><i></i></td>";
					html += "<td>";
					html += "<div class='fl'>";
					html += "<img src='/docs/"+list[i].prodImg+"' alt=''/>";
					html += "</div>";
					html += "<div class='detail fl'>";
					html += "<p>";
					html += "<span>"+list[i].prodCode+"</span>";
	                html += "</p>";
	                html += "<p>";
	                html += "<span>"+list[i].prodName+"</span>";
	                html += "</p>";
	                html += "</div>";
	                html += "</td>";
	                html += "<td>";
	                for(var j = 0;j<list[i].specs.length;j++){
	                	html += "<span>"+list[i].specs[j].specName+"："+list[i].specs[j].specValue+"；</span>";
	                }
	               	html += "</td>";
	                html += "<td class='price'>￥<span>"+list[i].price+"</span></td>";
	                html += "<td class='count'>";
	                html += "<div><b class='minus fl'></b><input class='productCount' onkeyup='value=value.replace(/[^\\d]/g,\"\")' type='text' value='"+(Number(list[i].quantity)).toFixed(2)+"'/><b class='add fr'></b></div>";
	                html += "<span>根</span>";
	                html += "<input type='text' class='minCount hidden' value='"+list[i].moq+"'/>";
	                html += "<input type='text' class='maxCount hidden' value='"+list[i].warehouseNum+"'/>";
	                html += "</td>";
	                html += "<td class='subtotal'><span data-money="+list[i].price*list[i].quantity+">"+cc((list[i].price*list[i].quantity).toString())+"</span></td>";
	                html += "<td>";
	                html += "<p class='delete_item'>移除</p>";
	                html += "<p>加入我的收藏</p>";
	                html += "</td>";
	                html += "</tr>"
				}
				$('.product-item').find('table').html(html);
			}
			
		},
		error:function(){
		}
	});
  //删除选种商品
    $('.delete').click(function(){
   		var item=$('table i');
   		var id=[];
   		$(item).each(function(){
   			if($(this).hasClass('selected')){
   				id.push($(this).parent().parent().attr('data-id'));
   			}
   		})
   		if(id.length == 0){
   			layer.msg("请选择要删除的商品", {icon: 2,time:1000});
   		}else{
   			var priceIds = id.join(",");
   		}
   		console.log(priceIds);
   		 $.ajax({
   			type:"POST",
   			url:path+"/orderlang/userCart/remove",
   			data:{"id":priceIds},
   			success:function(data){
   				console.log(data);
   				if(data.state == 1){
   					layer.msg("删除成功", {icon: 1,time:1000},function(){
   						location.reload();
   					});
   				}
   			}
   		});
    });
   // 单个删除
    $('.product-item').on('click','.delete_item',function(){
   	 var id = $(this).parent().parent().attr('data-id');
   	 $.ajax({
   			type:"POST",
   			url:path+"/orderlang/userCart/remove",
   			data:{"id":id},
   			success:function(data){
   				console.log(data);
   				if(data.state == 1){
   					layer.msg("删除成功", {icon: 1,time:1000},function(){
   						location.reload();
   					});
   				}
   			}
   		});
    })
}
init();
 function subTotal(){
     var totalMoney=Number(0);
     var item=$('table i');
     $(item).each(function(){
         if($(this).hasClass('selected')){
             totalMoney += Number($(this).parent().parent().find('.subtotal').children('span').attr('data-money'));
         }
     });
	     $('.table-foot').find('.subMoney').children().children().html(cc(totalMoney.toString()));
     if(totalMoney>0){
         $('.check').css({'color':'#fff','background':'#F23939'}).addClass('go-check');
     }else{
         $('.check').css('background','#BEBBBB').removeClass('go-check');
     }
 }
 function isPositiveInteger(s){//是否为正整数
     var re = /^[0-9]+$/ ;
     return re.test(s)
 }
 $('table').on('blur','input',function(){
	 	var max=Number($(this).parent().siblings('.maxCount').val());
	 	var min=Number($(this).parent().siblings('.minCount').val());
		if(isPositiveInteger(Number($(this).val()))){
			if(Number($(this).val()) > max){
				layer.msg("商品数量不得大于库存数量", {icon: 2,time:1000});
				$(this).val(Number(max).toFixed(2));
			}else if(Number($(this).val()) < min){
				layer.msg("商品数量不得小于起订量", {icon: 2,time:1000});
				$(this).val(Number(min).toFixed(2));
			}
			var count = $(this).val();
			var price = Number($(this).parent().parent().siblings('.price').find('span').html());
			$(this).parent().parent().siblings('.subtotal').find('span').html(cc((count*price).toString())).attr('data-money',count*price);
			subTotal();
		}else{
			layer.msg("请输入正确的商品数量", {icon: 2,time:1000});
		}
	})
 

